package com.github.data_manage.service

import com.github.data_manage.dao.ContrastBusinessDataDao
import com.github.data_manage.dao.ContrastDao
import com.google.genai.Client
import com.google.genai.types.GenerateContentConfig
import com.google.genai.types.GenerateContentResponse
import com.google.genai.types.GoogleSearch
import com.google.genai.types.Tool
import com.openai.client.OpenAIClient
import com.openai.client.okhttp.OpenAIOkHttpClient
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component


/**
 *
 * @author chenhainan
 * @since 2025/9/30
 */
@Component
class SpiderService(
    private val contrastDao: ContrastDao,
    private val contrastService: ContrastService,
    private val contrastBusinessDataDao: ContrastBusinessDataDao
) {

    companion object {

        private val logger = LoggerFactory.getLogger(SpiderService::class.java)

    }


    @Value($$"${system.openai-key}")
    lateinit var openAiKey: String

    @Value($$"${system.gemini-key}")
    lateinit var geminiKey: String

//    val openAiClient: OpenAIClient = OpenAIOkHttpClient.builder()
//        .apiKey(openAiKey)
//        .build()

    val googleClient: Client by lazy {
        Client.builder()
            .apiKey(geminiKey)
            .build()
    }

    fun contrastMonthValueSpider() {
        Thread.startVirtualThread {
            val contrast = contrastDao.findAll().first()
//            for (contrast in contrastDao.findAll()) {
            val config = GenerateContentConfig.builder()
                .tools(listOf(Tool.builder().googleSearch(GoogleSearch.builder().build()).build()))
                .build()
            val response: GenerateContentResponse =
                googleClient.models.generateContent(
                    "gemini-2.5-flash",
                    "把指数${contrast.name}最新的月度百分比变化数据,以{\"month\": \"2025-01\",\"value\": 0.0}的格式输出",
                    config
                )
            logger.info("Generate Contrast {} value {}", contrast.name, response.text())
            Thread.sleep(1000)
//            }
        }


    }
}